<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/6/28
 * Time: 14:56
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\admin\controller\machine;

use app\admin\traits\Curd;
use app\common\controller\AdminController;
use app\common\lib\Show;
use app\common\lib\Status;
use app\common\model\MachineMachine;
use app\common\model\SpecsSeasoning;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="机器口味管理")
 */
class MachineSeasoning extends AdminController
{
    use Curd;
    /**
     * 允许修改的字段
     * @var array
     */
    protected $allowModifyFields = [
        'status',
        'sort',
        'remark',
        'is_delete',
        'is_auth',
        'title',
        'percentage',
        'data_value',
        'value'
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\common\model\MachineSeasoning();

    }

    /**
     * @NodeAnotation(title="机器口味列表")
     * @return array|mixed|\think\response\Json
     * @throws \think\Exception
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $machine_id = $this->request->param('machine_id','','trim');
            if(empty($machine_id)){
                $this->error("内部异常！请刷新重试");
            }
            $res_data = $this->model->getPaginateByNormalListInfo(['machine_id'=>$machine_id],10000);

            return Show::json_show(Status::lay_normal,"ok",$res_data['data'],$res_data['total']);
        }
        $id = $this->request->param('id', 0, 'intval');
        //查询调料库是否存在
        $machine = (new MachineMachine())->getBaseById($id);
        if (!$machine) {
            $this->error("机器不存在");
        }
        //查询机器调料是否存在
        $machineSeasoning = $this->model->getBaseByListFind([
            "machine_id" => $machine['machine_id']
        ]);
        if (!$machineSeasoning) {
            //添加入库
            $seasoning = (new SpecsSeasoning())->getBaseByListAll();
            $insert_data = [];
            if (!empty($seasoning)) {
                foreach ($seasoning as $v) {
                    $insert_data[] = [
                        "title" => $v['title'],
                        "value" => $v['value'],
                        "machine_id" => $machine['machine_id'],
                        "create_time" => time(),
                        "update_time" => time()
                    ];
                }
            }
            $res = $this->model->insertAll($insert_data);
            if (!$res) {
                $this->error("操作失败！请重试");
            }
        }

        return $this->fetch('',[
            "machine_id"=>$machine['machine_id']
        ]);
    }

    /**
     * @NodeAnotation(title="机器口味添加")
     * @return void
     * @throws \think\Exception
     */
    public function add($id = null)
    {
        if($this->request->isAjax()){
            $post = $this->request->post();
            $validate = new \app\common\validate\MachineSeasoning();
            if(!$validate->check($post)){
                $this->error($validate->getError());
            }
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $pidMenuList = $this->model->getBaseByListAll(['pid'=>0]);
        $pidMenuList = array_merge([[
            'id' => 0,
            'pid' => 0,
            'title' => '顶级菜单',
        ]], $pidMenuList);
        if(!empty($id)){
            $row = $this->model->getBaseById(intval($id));
            if(!$row){
                $this->error("机器不存在");
            }
            $machine_id = $row['machine_id'];
        }else{
            $machine_id = $this->request->param('machine_id');
        }
        return $this->fetch('',[
            "pidMenuList"=>$pidMenuList,
            "id"=>$id,
            "machine_id"=>$machine_id
        ]);
    }

    /**
     * @NodeAnotation(title="机器口味编辑")
     * @param $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('row', $row);
        $pidMenuList = $this->model->getBaseByListAll(['pid'=>0]);
        $pidMenuList = array_merge([[
            'id' => 0,
            'pid' => 0,
            'title' => '顶级菜单',
        ]], $pidMenuList);
        $this->assign('pidMenuList',$pidMenuList);
        return $this->fetch();
    }
}